package cn.gyx.hrm.service.impl;

import cn.gyx.hrm.config.RabbitMQConfig;
import cn.gyx.hrm.constants.BaseConstants;
import cn.gyx.hrm.doc.CourseDoc;
import cn.gyx.hrm.domain.Course;
import cn.gyx.hrm.domain.CourseDetail;
import cn.gyx.hrm.domain.CourseMarket;
import cn.gyx.hrm.domain.CourseType;
import cn.gyx.hrm.dto.CourseDto;
import cn.gyx.hrm.mapper.CourseDetailMapper;
import cn.gyx.hrm.mapper.CourseMapper;
import cn.gyx.hrm.mapper.CourseMarketMapper;
import cn.gyx.hrm.service.ICourseMarketService;
import cn.gyx.hrm.service.ICourseService;
import cn.gyx.hrm.to.EmailTo;
import cn.gyx.hrm.to.SMSTo;
import cn.gyx.hrm.to.SystemTo;
import cn.gyx.hrm.util.AjaxResult;
import cn.gyx.hrm.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.gyx.hrm.fegin.client.* ;
import cn.gyx.hrm.fegin.client.CourseESFeignClient;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import static cn.gyx.hrm.constants.RabbitMQConstants.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gyx
 * @since 2021-11-26
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Autowired
    private CourseDetailMapper courseDetailMapper;

    @Autowired
    private CourseTypeServiceImpl courseTypeService;

    @Autowired
    private CourseESFeignClient courseESFeignClient;

    @Autowired
    private ICourseMarketService courseMarketService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void insertCouser(@Valid CourseDto dto) {
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();
//        1.参数基本判断，

//        重复值判断（根据课程名字查询判断是否重复）
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("name", course.getName());
        Course one = super.selectOne(wrapper);
        AssertUtil.isNull(one, "课程名重复请重命名");
//        2.保存课程
        // 由于还没有做登录所以这里先将保存课程的user信息写死 //TODO
        course.setStatus(BaseConstants.Course.COURSE_DOWN);// 设置默认状态为未上线
        course.setUserId(42L);
        course.setTenantId(26L);
        course.setUserName("yhptest1");
        course.setTenantName("源码时代");
        baseMapper.insert(course);

//        3.保存营销
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);

//        4.保存详情
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
//        5.可课程分类数+1
        CourseType courseType = courseTypeService.selectById(course.getCourseTypeId());
        courseType.setTotalCount(courseType.getTotalCount() + 1);
        courseTypeService.updateById(courseType);
    }


    /**
     * 课程上线
     *
     * @param id
     */
    @Override
    public void onLineCourse(Long id) {
        //1.查询课程id
        Course course = super.selectById(id);
        //2.判断课程状态必须是下架 断言判断
        AssertUtil.isEquals(course.getStatus(), BaseConstants.Course.COURSE_DOWN, "状态是下架才能上架");
        //3.修改课程状态为上架
        course.setStatus(BaseConstants.Course.COURSE_UP);
        // 更心上架时间
        course.setStartTime(new Date());
        // 更心数据库
        super.updateById(course);

        //4.把课程保存到Es中
        CourseMarket courseMarket = courseMarketService.selectById(id);

        CourseDoc doc = new CourseDoc();
        BeanUtils.copyProperties(courseMarket, doc);
        BeanUtils.copyProperties(course, doc);
        doc.setChargeName(courseMarket.getCharge() == 1 ? "免费" : "收费");
        doc.setOnlineTime(new Date());

        AjaxResult result = courseESFeignClient.save(doc);
        AssertUtil.isTrue(result.isSuccess(), "失败");

        pushMessage(course);
    }
    public void pushMessage (Course course) {
        String mesSMS = "亲爱的用户您好，您预约的["+course.getName()+"]已经上线啦，请尽快前往体验哦";
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_TOPIC,
                "course.sms",
                new SMSTo(Arrays.asList("123123,123132"),mesSMS)
        );
        String mesSystem = "亲爱的用户您好，您预约的["+course.getName()+"]已经上线啦，请尽快前往体验哦";
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_TOPIC,
                "course.system",
                new SystemTo(Arrays.asList(1L,2L),"66",mesSystem)
        );
        String mesEmail = "亲爱的用户您好，您预约的["+course.getName()+"]已经上线啦，请尽快前往体验哦";
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_TOPIC,
                "message.email",
                new EmailTo(Arrays.asList("132@123"),"77",mesEmail)
        );
    }

}
